Welcome to JavaScript functions and
prototypes. After watching this video, you will be
able describe functions in JavaScript, describe prototypes in JavaScript, and
demonstrate how to create a custom object. A function is a block of code which can
be called from any point in a script after being declared. A function is made up of the following
parts, the keyword function, the name of the function, parentheses
with optional parameter arguments, curly braces with the logic. The last statement in a function block
is the optional return statement that returns control back to
whatever called the function. This example is a function
that is named add. This function takes two parameter
arguments and returns the sum of the arguments or const concatenates
the two arguments if they are strings. Notice that you do not specify the data
type for the arguments of the function. The data types are determined by
the values of the arguments that are being passed to the function. There is no specific return type declared. The function returns
whatever type is required. In this case,
the return is a simple addition or concatenation of the input parameters. The action chosen depends on what
data is provided to the function. If the values can be interpreted
as numbers, they are added. If they can be interpreted as strings,
they are concatenated. This is an example of declaring
a function named Car that accepts three arguments as parameters. A little further down, you specifically
ask the function to run by declaring the statement var c = new Car with the
arguments Meridian, Saber GT, and 2012. In the Car function, the keyword this
refers to the current instance of the Car object that is being created. In other words, an instance of car that
is associated with the variable named c. Using the this keyword can differentiate
whether you are referring to the global or local instance of a variable. The use of model in this example refers
to the argument that was passed into the function. While the use of this model refers to the
model property associated with the current instance of the car object, the getname
function of car returns the make, model, and
year of the newly created car object. Prototypes allow you to
easily define properties and methods for
all instances of a particular object. All JavaScript objects which can be
created with the new keyword inherit properties and methods from a prototype. For example, the Car object we
created previously inherits the make, model, and year properties
from the constructor function, which are implicitly defined
in the Car prototype. When a car object is created, that
object inherits all the properties and methods that are defined
by this prototype. Unlike objects, the methods and properties of prototypes can
be changed with one call. So if you want to add a new property to an
objects constructor function, for example, you must add it to the function directly
by adding it as an additional argument. It cannot be done simply by
calling the object itself. With a prototype, however,
it is possible to add a property or method with just one call. Since all objects have
a prototype property, this is an easier way to add
properties and functions to objects. Any object that gets instantiated inherits
the current state of the prototype. If a prototype changes,
all objects using it will automatically inherit the new properties and
functions within that prototype. One way to change a prototype is
via a script which can override prototype properties and functions. Here is another example that uses
a prototype to change car instances. This time you add a method function
called getname to the car prototype. Now, when a car object is instantiated, it also includes the getname function
that returns the make, model, and year. All existing instances of the Car
object also inherit the getName method. Functions are usually declared first and
not run until you specifically ask them to, as you saw
in some of the previous examples. Auto-invocation or self-executing functions start running
immediately after being declared. The functions and
variables inside self executing functions are only available to the code
inside the self executing function. Auto-invocation functions can also be
unnamed or anonymous functions and have the format that is shown
in the code block on the slide. Self-executing functions are often
used to initialize data or to declare DOM elements on the page. In this video, you learned a function
is a block of code which can be called from any point in
a script after being declared. Functions can take arguments passed
as parameters and can return results. By using prototypes, you can easily
define properties and methods for all instances of a specific object. Prototypes exist for all JavaScript objects that can
be created with the new keyword. To add a new function to the template for the object, modify the prototype for
the object. Self-executing or auto-invocation functions start running
immediately after they have been declared. The functions and variables are isolated
from the rest of the script.